package com.delio.client.produccion.maquinarias;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.custom.Portal;
import com.extjs.gxt.ui.client.widget.custom.Portlet;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;

public class MaquinariaForm extends LayoutContainer{
		private DelioConstants x = GWT.create(DelioConstants.class);
		EditorGrid<ModelData> gMaq,gund;
		Grid<ModelData> gart;
		Button btnSaveMaq,btnNewMaq,btnDeleteMaq,btnSaveUND,btnNewUND,btnDeleteUND,btnSaveART,btnNewART,btnDeleteART,btnUpdateART,guardarART,salirART;
		private BeanModel selectedModelMaq;
		TextField<String>  txtDescripcionMaq,txtCodigoMaq;
		FormBinding fbmaq;
		Dialog dl;
		FormPanel fp;
		HiddenField<String> id;
		ComboBox<ModelData> maq;
		Integer idmaq;
	    PagingToolBar toolBarPagMAQ;
		
		
		public MaquinariaForm(){
			
		    setLayout(new BorderLayout());  
		    LayoutContainer north = new LayoutContainer();  
		    BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 30);  
		  
		    Portal portal = new Portal(1);  
		    portal.setBorders(true);  
		    portal.setStyleAttribute("backgroundColor", "SkyBlue");  
		    portal.setColumnWidth(0, .50);  
		 			      
		   	Portlet portlet = new Portlet();  
			Maquinarias.listarmaquinarias();
		    portlet.setHeading("Maquinarias"); 
		    portlet.setLayout(new FitLayout());
		    portlet.add(createGridMaq());
		    configPanelMaq(portlet);  
		    portlet.setHeight(500);
		    portal.add(portlet, 0);  
		    
		    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
		    centerData.setMargins(new Margins(5));  
		  
		    add(north, northData);  
		    //add(west, westData);  
		    add(portal, centerData);  
		  }  
		   	
		  public EditorGrid<ModelData> createGridMaq() {  

			  	List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
				
				CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
			    conf.add(sm.getColumn());
			    
			    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",250);

			    txtDescripcionMaq = new TextField<String>();  
			    txtDescripcionMaq.setAllowBlank(false);  
			    col.setEditor(new CellEditor(txtDescripcionMaq));  
			    conf.add(col);  

			   // txtCodigoMaq = new TextField<String>();  
			   // txtCodigoMaq.setAllowBlank(false);
			    col = new ColumnConfig("codigo","Codigo Maquinaria",100);
			    conf.add(col); 
			    
			  
			    ColumnModel cm = new ColumnModel(conf); 

			    gMaq = new EditorGrid<ModelData>(Maquinarias.store, cm); 
			    gMaq.setLoadMask(true); 
			    gMaq.setBorders(true); 
			    gMaq.addPlugin(sm);

			    gMaq.setSelectionModel(sm);
			    gMaq.getSelectionModel().addListener(Events.SelectionChange,
			       new Listener<SelectionChangedEvent<ModelData>>() {
					@Override
					public void handleEvent(SelectionChangedEvent<ModelData> be) {
					  if(be.getSelection().size()==0){
						//Maquinarias.loader.load();  
						btnDeleteMaq.setEnabled(false);
						btnSaveMaq.setEnabled(false);
						btnNewMaq.setEnabled(true);
				       }
					  if(be.getSelection().size()==1){
					 // be.getSelection().get(0);
					    selectedModelMaq = (BeanModel) be.getSelectedItem();
					    Maquinarias.maquinaria_id = selectedModelMaq.get("id");
					    System.out.println("el id del maquinaria "+Maquinarias.maquinaria_id);
				        btnDeleteMaq.setEnabled(true);
					    btnSaveMaq.setEnabled(true);
						btnNewMaq.setEnabled(false);
					  }
			 		}
			    });	
			    gMaq.setBorders(true);  
			    return gMaq;  
			  }    	
				
			private void configPanelMaq(final ContentPanel panel) {  
				  panel.setCollapsible(true);  
				  panel.setAnimCollapse(false);  
				  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
				  panel.getHeader().addTool(  
				      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
				        @Override  
				         public void componentSelected(IconButtonEvent ce) {  
				           panel.removeFromParent();  
				         }  
				     }));  
				   panel.setTopComponent(toolbarMaq());
			    }
				
				public ToolBar toolbarMaq(){
				  ToolBar toolBar = new ToolBar(); 
				  btnNewMaq = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
				  btnSaveMaq = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
				  btnDeleteMaq = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
				  btnSaveMaq.setEnabled(false);
				  btnDeleteMaq.setEnabled(false);
				  
				  toolBar.add(btnNewMaq);
				  toolBar.add(btnSaveMaq);
				  toolBar.add(btnDeleteMaq);
			      
			      btnDeleteMaq.addSelectionListener(new SelectionListener<ButtonEvent>(){
			       	public void componentSelected(ButtonEvent ce){
		       		if(Maquinarias.maquinaria_id!= null){
			       		System.out.println("tttttttttooooooooooodoooooooo");
						Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
						  public void handleEvent(MessageBoxEvent be) {
					        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
						       Maquinarias.eliminar(Maquinarias.maquinaria_id);
						       Maquinarias.loader.load();
						      }
					        }
						  };
						    String msg = "Esta seguro que desea eliminar este Articulos?";
						    MessageBox.confirm(x.eliminarCliente(), msg, callback);
						  }else{
						    Maquinarias.store.remove(0);
						}
			       	   }
			    	});
			      btnNewMaq.addSelectionListener(new SelectionListener<ButtonEvent>(){
			    	public void componentSelected(ButtonEvent ce){
			    	    Maquinaria m = new Maquinaria();
						m.setDescripcion("");
						m.setCodigo("");
						BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(m.getClass());
						gMaq.stopEditing();
					
						Maquinarias.store.insert(beanModelFactory.createModel(m), 0);
						gMaq.startEditing(Maquinarias.store.indexOf(beanModelFactory.createModel(m)), 0);
			    	}
			    	});
			        btnSaveMaq.addSelectionListener(new SelectionListener<ButtonEvent>(){
			    	public void componentSelected(ButtonEvent ce){
			    		Maquinaria  m = new Maquinaria();
				 		m.setDescripcion(txtDescripcionMaq.getValue());
				 		m.setCodigo("");
				 		btnSaveMaq.setEnabled(false);	  
				 		if(Maquinarias.maquinaria_id==null){
				 		  Maquinarias.guardar(m);
				 		}
				 		if(Maquinarias.maquinaria_id!=null){
				 		  m.setId(Maquinarias.maquinaria_id);
					 	  Maquinarias.actualizar(m);
				 		}
				 		 Maquinarias.store.commitChanges();
				 		 Maquinarias.loader.load();
				 	}
			       });
			     return toolBar;
	    }	
		 
}